package com.tencent.sr.iris.activity.service.activity;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.tencent.sr.iris.activity.dependency.user.UserCoreDependency;
import com.tencent.sr.iris.activity.interfaces.enums.ActivityEventSourceEnum;
import com.tencent.sr.iris.activity.interfaces.enums.ActivityEventTypeEnum;
import com.tencent.sr.iris.activity.interfaces.event.dto.ActivityEventDTO;
import com.tencent.sr.iris.activity.interfaces.request.acticity.UserOpenAppRequest;
import com.tencent.sr.iris.activity.service.constant.ActivityRedisKey;
import com.tencent.sr.iris.activity.service.event.EventStandardService;
import com.tencent.sr.iris.activity.service.util.IdCreateorUtil;
import com.tencent.sr.iris.user.core.service.interfaces.enums.UserLevelEnum;
import com.tencent.sr.iris.user.core.service.interfaces.response.user.UserInfoQueryResponse;
import com.tencent.sr.rmall.springbootstarter.redis.RedisClient;
import java.time.LocalDate;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 用户事件上报
 *
 * @author xiaojun
 */
@Slf4j
@Service
public class UserEventReportService {

    @Resource
    private IdCreateorUtil idCreateorUtil;
    @Resource
    private UserCoreDependency userCoreDependency;
    @Resource
    private EventStandardService eventStandardService;
    @Resource
    private RedisClient redisClient;

    public Boolean userOpenApp(UserOpenAppRequest request) {

        //每个uid每天只能上报一次
        String userOpenAppTagKey = ActivityRedisKey.getUserOpenAppTagKey(request.getUid());
        try {
            String uid = request.getUid();

            String res = redisClient.set(userOpenAppTagKey, "1", "NX", "EX", 24 * 3600);
            if (Strings.isNullOrEmpty(res)) {
                log.info("用户打开小程序事件上报，今日已上报过，忽略本次上报，uid:{}", uid);
                return true;
            }

            UserInfoQueryResponse userInfo = userCoreDependency.queryUserInfo(uid, false);
            if (userInfo == null || userInfo.getUserInfoDTO() == null) {
                log.info("用户打开小程序事件上报，查不到用户信息，uid:{}, userInfo:{}", uid, JSON.toJSON(userInfo));
                return true;
            }
            if (!Lists.newArrayList(UserLevelEnum.TRAINEE.getValue())
                    .contains(userInfo.getUserInfoDTO().getLevelId())) {
                log.info("用户打开小程序事件上报，用户不是见习会员，忽略上报用户打开小程序事件，uid:{},levelId:{}", uid,
                        userInfo.getUserInfoDTO().getLevelId());
                return true;
            }

            ActivityEventDTO event = new ActivityEventDTO();
            event.setEventType(ActivityEventTypeEnum.USER_OPEN_MINI_PROGRAM.getCode());
            event.setEventTime(System.currentTimeMillis());
            event.setEventInfo("{}");
            event.setEventSource(ActivityEventSourceEnum.USER_REPORT.getCode());
            event.setIdempotent(idCreateorUtil.genId());
            event.setCreateTime(System.currentTimeMillis());
            event.setUid(uid);

            eventStandardService.sendStandardEventMsg(event);
            log.info("用户打开小程序事件上报，成功上报，uid:{},event:{}", uid,
                    JSON.toJSONString(event));
        } catch (Exception e) {
            log.error("用户打开小程序事件上报，异常，uid:{}", request.getUid(), e);
            redisClient.del(userOpenAppTagKey);
        }
        return true;
    }


}
